﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using Telerik.Web.UI;

namespace RN.ERP.Web.Person
{
    public partial class PersonEmpCertDetail : BasePage
    {
        private int codeType = 0;
        /// <summary>
        /// 页面编辑状态
        /// </summary>
        protected int EditStatus
        {
            get;
            set;
        }
        /// <summary>
        /// 用户类型
        /// </summary>
        protected string UserRole
        {
            get
            {
                return UserInfo.RoleType;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetParams();
                CheckCardTypeAndShow();
                if (UserInfo.RoleType == "User")
                {
                    scBtnDiv.Visible = true;
                    btnPostDiv.Visible = true;
                }
                else
                {
                    PrintbtnBtnDiv.Visible = true;
                }
                GetCardFlowCodes();
            }
        }
        /// <summary>
        /// 取得参数
        /// </summary>
        private void GetParams()
        {
            Guid empGuid = Guid.Empty;
            int opt = 0;
           
            if (!string.IsNullOrEmpty(Request.QueryString["empGuid"]))
            {
                Guid.TryParse(Request.QueryString["empGuid"], out empGuid);
            }
            hdEmpGuid.Value = empGuid.ToString();
            ////类型
            if (!string.IsNullOrEmpty(Request.QueryString["codeType"]))
            {
                int.TryParse(Request.QueryString["codeType"], out codeType);
            }
            if (Request.QueryString["objectName"] != null)
            {
                lblempName.Text = Request.QueryString["objectName"];
            }
            if (Request.QueryString["corpName"] != null)
            {
                lblCorpName.Text = Request.QueryString["corpName"];
            }
            ////可编辑显示状态（1：内容不可编辑，2：所有不可用）
            if (!string.IsNullOrEmpty(Request.QueryString["opt"]))
            {
                int.TryParse(Request.QueryString["opt"], out opt);
            }
            hdCorpGuid.Value = UserInfo.CorpUserGuid.ToString();
            HdAreaCode.Value = UserInfo.CurrentArea;
           
        }

        /// <summary>
        /// 判断证书类型并显示
        /// </summary>
        private void CheckCardTypeAndShow()
        {
            if (codeType == 251201)//人员执业手册
            {
                CheckEmpManualIsExist();
            }

        }
        /// <summary>
        /// 判断是否显示人员手册并取得人员手册标识
        /// </summary>
        private void CheckEmpManualIsExist()
        {
            using (PersonService.PersonServiceClient client = new PersonService.PersonServiceClient())
            {
                var result = JsonConvert.DeserializeObject<ModelService.OperationResult>(client.CheckEmpManualIsExist(UserInfo.CorpUserGuid, hdEmpGuid.Value.Trim(), "ZGZS", HdAreaCode.Value.Trim()));
                if (result.Result)
                {
                    hdManual.Value = result.ReturnValue.ToString();
                }
            }
        }
        /// <summary>
        /// 判断审核状态
        /// </summary>
        private void GetCardFlowCodes()
        {
            using (PersonService.PersonServiceClient client = new PersonService.PersonServiceClient())
            {
                var result = JsonConvert.DeserializeObject<ModelService.OperationResult>(client.GetCardFlowCodes(UserInfo.CorpUserGuid, hdEmpGuid.Value.Trim(), string.Empty, 251201, 7, UserInfo.CurrentArea));
                if (result.Result)
                {
                    List<ModelService.OperationResult> resultList = JsonConvert.DeserializeObject<List<ModelService.OperationResult>>(result.ReturnValue.ToString());
                   
                    if (resultList[5].Result || resultList[6].Result)
                    {
                        if (UserInfo.RoleType == "Manager")
                        {
                            btnShDiv.Visible = true;//审核
                        }
                    }

                }
            }
        }

        #region 项目总代操作

        #region 取得在建项目数据源
        /// <summary>
        /// 查询条件
        /// </summary>
        /// <param name="currentPageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="empId"></param>
        /// <param name="areaCode"></param>
        /// <returns></returns>
        private ProjectService.ProjectQueryCodition GetCondition(int currentPageIndex, int pageSize, Guid empGuid, string areaCode)
        {
            ProjectService.ProjectQueryCodition condition = new ProjectService.ProjectQueryCodition();
            condition.PageNum = currentPageIndex;
            condition.PageSize = pageSize;
            //condition.bStatus = 254;//在建工程（h_projectbzk_new）
            condition.IsrtAreaCode = areaCode;
            condition.CorpType = 42;
            condition.EmpGuid = empGuid;
            condition.isperson = true;
            return condition;
            // return JsonConvert.SerializeObject(condition); 
        }
        /// <summary>
        /// 取得列表
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        private List<BzkService.BzkProject> GetEmpprojectList(ProjectService.ProjectQueryCodition condition, out int count)
        {
            try
            {
                using (ProjectService.ProjectServiceClient bzkClient = new ProjectService.ProjectServiceClient())
                {
                    string listStr = bzkClient.GetProjectListQD(out count, condition);
                    return JsonConvert.DeserializeObject<List<BzkService.BzkProject>>(listStr);
                }
            }
            catch
            {
                count = 0;
                return null;
            }
        }
       
        #endregion

        #region 在建项目数据绑定
        /// <summary>
        /// 绑定总代项目数据列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void rgPersonList_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            rgPersonDataBind();
        }
       
        /// <summary>
        /// 绑定总代项目数据源
        /// </summary>
        private void rgPersonDataBind()
        {
            Guid empGuid = Guid.Empty; ;
            if (Guid.TryParse(hdEmpGuid.Value.Trim(), out empGuid))
            {
                int currentPageIndex = rgPersonList.CurrentPageIndex;
                int pageSize = rgPersonList.MasterTableView.PageSize;
                var queryStr = GetCondition(currentPageIndex, pageSize, empGuid, UserInfo.CurrentArea);
                int count = 0;
                var list = GetEmpprojectList(queryStr, out count);
                rgPersonList.VirtualItemCount = count;
                rgPersonList.DataSource = list;
            }
        }
      
        #endregion

        /// <summary>
        /// 人员类型转换
        /// </summary>
        /// <param name="managertype"></param>
        /// <returns></returns>
        public string TransManagerType(object managertype)
        {
            if (managertype != null)
            {
                switch (managertype.ToString())
                {
                    case "0":
                        return "项目经理";
                    case "2":
                        return "项目总监";
                    case "45":
                        return "建设单位联系人";
                    case "100":
                        return "项目主管";
                    case "101":
                        return "项目经办人";
                    default:
                        return string.Empty;
                }
            }
            return string.Empty;
        }

        /// <summary>
        /// 返回项目状态
        /// </summary>
        /// <param name="bstatus"></param>
        /// <returns></returns>
        protected string GetProjectStatus(object bstatus)
        {
            if (bstatus != null)
            {
                switch (bstatus.ToString())
                {
                    case "2":
                        return "招标";
                    case "3":
                        return "施工许可办理";
                    case "10":
                        return "停工";
                    case "254":
                        return "在建";
                    case "255":
                        return "竣工";
                    default:
                        return string.Empty;
                }
            }
            return string.Empty;
        }
        #endregion
    }
}